package com.boilermaker.springbootdemo;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    private List<MessageInfo> messageInfoList = new ArrayList<>();

    @RequestMapping(value = "/publish", produces = "application/json")
    // 接收JSON使用 @RequestBody 注解
    public String publish(@RequestBody MessageInfo messageInfo) {
        if (!StringUtils.hasLength(messageInfo.getMessage()) &&
                !StringUtils.hasLength(messageInfo.getTo()) &&
                !StringUtils.hasLength(messageInfo.getFrom())) {
            // 返回JSON需要在 @RequestMapping 中手动设置produces
            return "{\"ok\" : 0}";
        }
        messageInfoList.add(messageInfo);
        return "{\"ok\" : 1}";
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageInfoList;
    }
}
